package com.cmc.cloud.cmclink.doc.service.impl.si;

import com.cmc.cloud.cmclink.doc.constants.errorconstant.ConfirmNoticeDetailErrorCodeConstants;
import com.cmc.cloud.cmclink.doc.service.si.ConfirmNoticeDetailService;
import com.cmc.cloud.cmclink.doc.vo.confirmnoticedetailvo.ConfirmNoticeDetailCreateReqVO;
import com.cmc.cloud.cmclink.doc.vo.confirmnoticedetailvo.ConfirmNoticeDetailPageReqVO;
import com.cmc.cloud.cmclink.doc.vo.confirmnoticedetailvo.ConfirmNoticeDetailUpdateReqVO;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import com.cmes.framework.common.pojo.PageResult;
import com.cmes.framework.common.exception.util.ServiceExceptionUtil;

import java.util.*;
import com.cmc.cloud.cmclink.doc.entity.ConfirmNoticeDetailDO;
import com.cmc.cloud.cmclink.doc.convert.ConfirmNoticeDetailConvert;
import com.cmc.cloud.cmclink.doc.mapper.ConfirmNoticeDetailMapper;


/**
 * 确认装货港更改通知子 Service 实现类
 *
 * @author 集运管理员
 */
@Service
@Validated
public class ConfirmNoticeDetailServiceImpl implements ConfirmNoticeDetailService {

    @Resource
    private ConfirmNoticeDetailMapper confirmNoticeDetailMapper;

    @Override
    public void createConfirmNoticeDetail(ConfirmNoticeDetailCreateReqVO createReqVO) {
        // 插入
        ConfirmNoticeDetailDO confirmNoticeDetail = ConfirmNoticeDetailConvert.INSTANCE.convert(createReqVO);
        confirmNoticeDetailMapper.insert(confirmNoticeDetail);
    }

    @Override
    public void updateConfirmNoticeDetail(ConfirmNoticeDetailUpdateReqVO updateReqVO) {
        // 校验存在
        validateConfirmNoticeDetailExists(updateReqVO.getId());
        // 更新
        ConfirmNoticeDetailDO updateObj = ConfirmNoticeDetailConvert.INSTANCE.convert(updateReqVO);
        confirmNoticeDetailMapper.updateById(updateObj);
    }

    @Override
    public void deleteConfirmNoticeDetail(Long id) {
        // 校验存在
        validateConfirmNoticeDetailExists(id);
        // 删除
        confirmNoticeDetailMapper.deleteById(id);
    }

    private void validateConfirmNoticeDetailExists(Long id) {
        if (confirmNoticeDetailMapper.selectById(id) == null) {
            throw ServiceExceptionUtil.exception(ConfirmNoticeDetailErrorCodeConstants.CONFIRM_NOTICE_DETAIL_NOT_EXISTS);
        }
    }

    @Override
    public ConfirmNoticeDetailDO getConfirmNoticeDetail(Long id) {
        return confirmNoticeDetailMapper.selectById(id);
    }

    @Override
    public List<ConfirmNoticeDetailDO> getConfirmNoticeDetailList(Collection<Long> ids) {
        return confirmNoticeDetailMapper.selectBatchIds(ids);
    }

    @Override
    public PageResult<ConfirmNoticeDetailDO> getConfirmNoticeDetailPage(ConfirmNoticeDetailPageReqVO reqVO) {
        IPage<ConfirmNoticeDetailDO> page = Page.of(reqVO.getPageNo(), reqVO.getPageSize());
        
        IPage<ConfirmNoticeDetailDO> result = confirmNoticeDetailMapper.selectPageList(page, reqVO);
        return new PageResult<ConfirmNoticeDetailDO>(result.getRecords(), result.getTotal());
    }
}
